<template>
  <el-dialog :title="$t('productCenter.appConfig.push.edit')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item :label="$t('productCenter.appConfig.push.theme')" prop="theme">
        <el-input
          v-model="formValidate.theme"
          readonly
        />
      </el-form-item>
      <el-form-item :label="$t('productCenter.appConfig.push.title')" prop="title">
        <el-input
          v-model="formValidate.title"
          clearable
          :placeholder="$t('productCenter.appConfig.push.prompt.title')"
          :maxlength="100"
          show-word-limit
        />
      </el-form-item>
      <el-form-item :label="$t('productCenter.appConfig.push.content')" prop="text">
        <el-input
          v-model="formValidate.text"
          type="textarea"
          :autosize="{ minRows: 6, maxRows: 10}"
          :placeholder="$t('productCenter.appConfig.push.prompt.content')"
          :maxlength="500"
          show-word-limit
        />
      </el-form-item>
      <el-form-item :label="$t('productCenter.appConfig.push.state')" prop="status">
        <el-select v-model="formValidate.status" :placeholder="$t('productCenter.appConfig.push.prompt.state')" style="width: 100%">
          <el-option
            v-for="item in stateData"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updatePushTemplate } from '@/api/product'
export default {
  name: 'SmsTemplateDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    maxOrder: {
      type: Number,
      default: 1
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      submitLoading: false,
      stateData: (this.$t('productCenter.appConfig.push.status')).slice(1),
      formValidate: {
        id: null,
        title: null,
        theme: null,
        text: null,
        status: null,
        templateCode: null
      },
      rules: {
        title: [{ required: true, message: this.$t('productCenter.appConfig.push.state') + this.$t('common.null'), trigger: 'blur' }],
        theme: [{ required: true, message: this.$t('productCenter.appConfig.push.theme') + this.$t('common.null'), trigger: 'blur' }],
        text: [{ required: true, message: this.$t('productCenter.appConfig.push.content') + this.$t('common.null'), trigger: 'blur' }],
        status: [{ required: true, message: this.$t('productCenter.appConfig.push.state') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs['formValidate'].clearValidate()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const data = this.formValidate
          updatePushTemplate(data)
            .then(res => {
              this.submitLoading = false
              if (res.success) {
                this.$message.success(this.$t('common.tip.editSuccess'))
                this.$emit('change-dialog', false)
                this.$emit('edit-success')
                this.handleClose()
              } else {
                this.$message.error(res.message)
              }
            })
            .catch(err => {
              this.$message.error(err.message)
              this.submitLoading = false
            })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
